package com.uking.util;

import android.content.Context;
import android.content.SharedPreferences.Editor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
    public static String subStringIp(String url) {
        Matcher m = Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}").matcher(url);
        if (m.find()) {
            return m.group();
        }
        return "";
    }

    public static String subString(String url) {
        String tmpString = "";
        int start = url.indexOf("//");
        int end = url.lastIndexOf(":");
        if (start == -1 || end == -1) {
            return tmpString;
        }
        return url.substring(start + 2, end);
    }

    public static String subStringApi(String url) {
        try {
            if (url.indexOf("?") != -1) {
                url = url.substring(0, url.indexOf("?"));
            }
            if (url.indexOf("api") != -1) {
                url = url.substring(url.indexOf("api"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

    public static void setIp(Context context, String ipString) {
        Editor mSettinsEd = context.getSharedPreferences("kbSharedPreferences", 0).edit();
        mSettinsEd.putString("ip", ipString);
        mSettinsEd.commit();
    }

    public static String getIp(Context context) {
        return context.getSharedPreferences("kbSharedPreferences", 0).getString("ip", "");
    }
}
